<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager_modules extends Admin_Controller {

	protected $table = ADMIN_MODULE_TABLE; // Also configed in model
	
	// Config List view
	protected $list_view_order_name = 'name';
	protected $list_view_order_direction = 'ASC';
	protected $list_view_searched_fields = array(
			'name'			=>	'name',
		);
		
	protected $fields =  array(
			'name'		=>	array(
								'text'		=>	'Group-Name',
								'type'		=>	'text',
								'list_view'=>array(
									'is_show_link'	=>	true
								),
								'form_view'=>array(
									'type'	=>	'text',
									'func'	=>	''
								)
							),
			/*'status'		=>	array(
								'text'	=>	'Action',
								'type'	=>	'checkbox',
								'list_view'=>array(
									'func'	=>	''
								),
								'form_view'=>array(
									'type'				=>	'checkbox',
									'func'				=>	''
								)
							),*/
			);
	/** End: Config **/

	/*------------------------------------ Admin Control Panel ------------------------------------*/
	
	public function admincp_ajaxUpdateStatus(){
		$perm = modules::run('admincp/chk_perm',$this->session->userdata('ID_Module'),'w',1);
		if($perm=='permission-denied'){
			print '<script type="text/javascript">show_perm_denied()</script>';
			$status = $this->input->post('status');
			$data = array(
				$this->status_name=>$status
			);
			$update = array(
				'status'=>$status,
				'id'=>$this->input->post('id'),
				'module'=>$this->module
			);
			$this->load->view('ajax_updateStatus',$update);
		}else{
			if($this->input->post('status')==0){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array(
				$this->status_name=>$status
			);
			$this->db->where('id', $this->input->post('id'));
			if($this->db->update($this->table, $data)){
				//Xét quyền cho Group
				$this->load->model('account_groups/account_groups_model');
				$list_group = $this->account_groups_model->list_groups();
				foreach($list_group as $v){
					if($status==0){
						$pos = strpos($v->permission,$this->input->post('id'));
						if($pos !== false){
							$sub_str = substr($v->permission,$pos-1,6);
							$new_perm = str_replace($sub_str,'',$v->permission);
							$this->db->where('id',$v->id);
							$this->db->update(ADMIN_GROUP_TABLE,array('permission'=>$new_perm));
						}
					}else{
						$pos = strpos($v->permission,'0');
						if($pos !== false){
							$new_perm = substr($v->permission,$pos+2,3);
							$this->db->where('id',$v->id);
							$this->db->update(ADMIN_GROUP_TABLE,array('permission'=>$v->permission.','.$this->input->post('id').'|'.$new_perm));
						}
					}
				}
				
				//Xét quyền lại cho từng Users
				$this->load->model('accounts/accounts_model');
				$list_account = $this->accounts_model->list_accounts();
				foreach($list_account as $v){
					if($status==0){
						$pos = strpos($v->permission,$this->input->post('id'));
						if($pos !== false){
							$sub_str = substr($v->permission,$pos-1,6);
							$new_perm = str_replace($sub_str,'',$v->permission);
							$this->db->where('id',$v->id);
							$this->db->update(ADMIN_USER_TABLE,array('permission'=>$new_perm));
						}
					}else{
						$group_account = $this->account_groups_model->getDetailManagement($v->group_id);
						$pos = strpos($group_account->permission,'0');
						if($pos !== false){
							$new_perm = substr($v->permission,$pos+2,3);
							$this->db->where('id',$v->id);
							$this->db->update(ADMIN_USER_TABLE,array('permission'=>$v->permission.','.$this->input->post('id').'|'.$new_perm));
						}
					}
				}
			}
			
			$update = array(
				'status'=>$status,
				'id'=>$this->input->post('id'),
				'module'=>$this->module
			);
			$this->load->view('ajax_updateStatus',$update);
		}
	}
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	/*------------------------------------ End FRONTEND --------------------------------*/
}